package org.hadoop.zk;
import org.apache.zookeeper.*;
import java.io.File;
import java.nio.file.Files;
import java.util.concurrent.CountDownLatch;
public class SaveDatas implements Watcher {
    public static void main(String[] args) throws Exception {
        new SaveDatas();
    }
    CountDownLatch countDownLatch = new CountDownLatch(1);
    public SaveDatas() throws Exception {
        ZooKeeper zk = new ZooKeeper("server101:2181", 2000, this);
        countDownLatch.await();
        File file = new File("./chapter07/src/test/java/org/hadoop/zk/SaveDatas.java");
        //获取这个文件的所有数据
        byte[] bs = Files.readAllBytes(file.toPath());
        //在创建目录的同时，将数据保存到这个节点中
        zk.create("/test/" + file.getName(), bs, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        zk.close();
    }
    @Override
    public void process(WatchedEvent event) {
        if (event.getState() == Event.KeeperState.SyncConnected && event.getType() == Event.EventType.None) {
            countDownLatch.countDown();
        }
    }
}
